

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>rl_coach.environments.doom_environment &mdash; Reinforcement Learning Coach 0.12.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
    <link href="../../../_static/css/custom.css" rel="stylesheet" type="text/css">

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> Reinforcement Learning Coach
          

          
            
            <img src="../../../_static/dark_logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Intro</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../usage.html">Usage</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../dist_usage.html">Usage - Distributed Coach</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../features/index.html">Features</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../selecting_an_algorithm.html">Selecting an Algorithm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../dashboard.html">Coach Dashboard</a></li>
</ul>
<p class="caption"><span class="caption-text">Design</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../design/control_flow.html">Control Flow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../design/network.html">Network Design</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../design/horizontal_scaling.html">Distributed Coach - Horizontal Scale-Out</a></li>
</ul>
<p class="caption"><span class="caption-text">Contributing</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../contributing/add_agent.html">Adding a New Agent</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../contributing/add_env.html">Adding a New Environment</a></li>
</ul>
<p class="caption"><span class="caption-text">Components</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../components/agents/index.html">Agents</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../components/architectures/index.html">Architectures</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../components/data_stores/index.html">Data Stores</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../components/environments/index.html">Environments</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../components/exploration_policies/index.html">Exploration Policies</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../components/filters/index.html">Filters</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../components/memories/index.html">Memories</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../components/memory_backends/index.html">Memory Backends</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../components/orchestrators/index.html">Orchestrators</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../components/core_types.html">Core Types</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../components/spaces.html">Spaces</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../components/additional_parameters.html">Additional Parameters</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Reinforcement Learning Coach</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>rl_coach.environments.doom_environment</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for rl_coach.environments.doom_environment</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1"># Copyright (c) 2017 Intel Corporation</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#      http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1">#</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">vizdoom</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">rl_coach.logger</span> <span class="k">import</span> <span class="n">failed_imports</span>
    <span class="n">failed_imports</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;ViZDoom&quot;</span><span class="p">)</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">enum</span> <span class="k">import</span> <span class="n">Enum</span>
<span class="kn">from</span> <span class="nn">os</span> <span class="k">import</span> <span class="n">path</span><span class="p">,</span> <span class="n">environ</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">Union</span><span class="p">,</span> <span class="n">List</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">rl_coach.base_parameters</span> <span class="k">import</span> <span class="n">VisualizationParameters</span>
<span class="kn">from</span> <span class="nn">rl_coach.environments.environment</span> <span class="k">import</span> <span class="n">Environment</span><span class="p">,</span> <span class="n">EnvironmentParameters</span><span class="p">,</span> <span class="n">LevelSelection</span>
<span class="kn">from</span> <span class="nn">rl_coach.filters.action.full_discrete_action_space_map</span> <span class="k">import</span> <span class="n">FullDiscreteActionSpaceMap</span>
<span class="kn">from</span> <span class="nn">rl_coach.filters.filter</span> <span class="k">import</span> <span class="n">InputFilter</span><span class="p">,</span> <span class="n">OutputFilter</span>
<span class="kn">from</span> <span class="nn">rl_coach.filters.observation.observation_rescale_to_size_filter</span> <span class="k">import</span> <span class="n">ObservationRescaleToSizeFilter</span>
<span class="kn">from</span> <span class="nn">rl_coach.filters.observation.observation_rgb_to_y_filter</span> <span class="k">import</span> <span class="n">ObservationRGBToYFilter</span>
<span class="kn">from</span> <span class="nn">rl_coach.filters.observation.observation_stacking_filter</span> <span class="k">import</span> <span class="n">ObservationStackingFilter</span>
<span class="kn">from</span> <span class="nn">rl_coach.filters.observation.observation_to_uint8_filter</span> <span class="k">import</span> <span class="n">ObservationToUInt8Filter</span>
<span class="kn">from</span> <span class="nn">rl_coach.spaces</span> <span class="k">import</span> <span class="n">MultiSelectActionSpace</span><span class="p">,</span> <span class="n">ImageObservationSpace</span><span class="p">,</span> \
    <span class="n">VectorObservationSpace</span><span class="p">,</span> <span class="n">StateSpace</span>


<span class="c1"># enum of the available levels and their path</span>
<span class="k">class</span> <span class="nc">DoomLevel</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
    <span class="n">BASIC</span> <span class="o">=</span> <span class="s2">&quot;basic.cfg&quot;</span>
    <span class="n">DEFEND</span> <span class="o">=</span> <span class="s2">&quot;defend_the_center.cfg&quot;</span>
    <span class="n">DEATHMATCH</span> <span class="o">=</span> <span class="s2">&quot;deathmatch.cfg&quot;</span>
    <span class="n">MY_WAY_HOME</span> <span class="o">=</span> <span class="s2">&quot;my_way_home.cfg&quot;</span>
    <span class="n">TAKE_COVER</span> <span class="o">=</span> <span class="s2">&quot;take_cover.cfg&quot;</span>
    <span class="n">HEALTH_GATHERING</span> <span class="o">=</span> <span class="s2">&quot;health_gathering.cfg&quot;</span>
    <span class="n">HEALTH_GATHERING_SUPREME_COACH_LOCAL</span> <span class="o">=</span> <span class="s2">&quot;D2_navigation.cfg&quot;</span>  <span class="c1"># from https://github.com/IntelVCL/DirectFuturePrediction/tree/master/maps</span>
    <span class="n">DEFEND_THE_LINE</span> <span class="o">=</span> <span class="s2">&quot;defend_the_line.cfg&quot;</span>
    <span class="n">DEADLY_CORRIDOR</span> <span class="o">=</span> <span class="s2">&quot;deadly_corridor.cfg&quot;</span>
    <span class="n">BATTLE_COACH_LOCAL</span> <span class="o">=</span> <span class="s2">&quot;D3_battle.cfg&quot;</span>  <span class="c1"># from https://github.com/IntelVCL/DirectFuturePrediction/tree/master/maps</span>

<span class="n">key_map</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;NO-OP&#39;</span><span class="p">:</span> <span class="mi">96</span><span class="p">,</span>  <span class="c1"># `</span>
    <span class="s1">&#39;ATTACK&#39;</span><span class="p">:</span> <span class="mi">13</span><span class="p">,</span>  <span class="c1"># enter</span>
    <span class="s1">&#39;CROUCH&#39;</span><span class="p">:</span> <span class="mi">306</span><span class="p">,</span>  <span class="c1"># ctrl</span>
    <span class="s1">&#39;DROP_SELECTED_ITEM&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;t&quot;</span><span class="p">),</span>
    <span class="s1">&#39;DROP_SELECTED_WEAPON&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;t&quot;</span><span class="p">),</span>
    <span class="s1">&#39;JUMP&#39;</span><span class="p">:</span> <span class="mi">32</span><span class="p">,</span>  <span class="c1"># spacebar</span>
    <span class="s1">&#39;LAND&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;l&quot;</span><span class="p">),</span>
    <span class="s1">&#39;LOOK_DOWN&#39;</span><span class="p">:</span> <span class="mi">274</span><span class="p">,</span>  <span class="c1"># down arrow</span>
    <span class="s1">&#39;LOOK_UP&#39;</span><span class="p">:</span> <span class="mi">273</span><span class="p">,</span>  <span class="c1"># up arrow</span>
    <span class="s1">&#39;MOVE_BACKWARD&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;s&quot;</span><span class="p">),</span>
    <span class="s1">&#39;MOVE_DOWN&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;s&quot;</span><span class="p">),</span>
    <span class="s1">&#39;MOVE_FORWARD&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;w&quot;</span><span class="p">),</span>
    <span class="s1">&#39;MOVE_LEFT&#39;</span><span class="p">:</span> <span class="mi">276</span><span class="p">,</span>
    <span class="s1">&#39;MOVE_RIGHT&#39;</span><span class="p">:</span> <span class="mi">275</span><span class="p">,</span>
    <span class="s1">&#39;MOVE_UP&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;w&quot;</span><span class="p">),</span>
    <span class="s1">&#39;RELOAD&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;r&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SELECT_NEXT_WEAPON&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;q&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SELECT_PREV_WEAPON&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;e&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SELECT_WEAPON0&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;0&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SELECT_WEAPON1&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;1&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SELECT_WEAPON2&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;2&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SELECT_WEAPON3&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;3&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SELECT_WEAPON4&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;4&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SELECT_WEAPON5&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;5&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SELECT_WEAPON6&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;6&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SELECT_WEAPON7&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;7&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SELECT_WEAPON8&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;8&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SELECT_WEAPON9&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;9&quot;</span><span class="p">),</span>
    <span class="s1">&#39;SPEED&#39;</span><span class="p">:</span> <span class="mi">304</span><span class="p">,</span>  <span class="c1"># shift</span>
    <span class="s1">&#39;STRAFE&#39;</span><span class="p">:</span> <span class="mi">9</span><span class="p">,</span>  <span class="c1"># tab</span>
    <span class="s1">&#39;TURN180&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;u&quot;</span><span class="p">),</span>
    <span class="s1">&#39;TURN_LEFT&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;a&quot;</span><span class="p">),</span>  <span class="c1"># left arrow</span>
    <span class="s1">&#39;TURN_RIGHT&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;d&quot;</span><span class="p">),</span>  <span class="c1"># right arrow</span>
    <span class="s1">&#39;USE&#39;</span><span class="p">:</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;f&quot;</span><span class="p">),</span>
<span class="p">}</span>


<span class="n">DoomInputFilter</span> <span class="o">=</span> <span class="n">InputFilter</span><span class="p">(</span><span class="n">is_a_reference_filter</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">DoomInputFilter</span><span class="o">.</span><span class="n">add_observation_filter</span><span class="p">(</span><span class="s1">&#39;observation&#39;</span><span class="p">,</span> <span class="s1">&#39;rescaling&#39;</span><span class="p">,</span>
                                       <span class="n">ObservationRescaleToSizeFilter</span><span class="p">(</span><span class="n">ImageObservationSpace</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">60</span><span class="p">,</span> <span class="mi">76</span><span class="p">,</span> <span class="mi">3</span><span class="p">]),</span>
                                                                                            <span class="n">high</span><span class="o">=</span><span class="mi">255</span><span class="p">)))</span>
<span class="n">DoomInputFilter</span><span class="o">.</span><span class="n">add_observation_filter</span><span class="p">(</span><span class="s1">&#39;observation&#39;</span><span class="p">,</span> <span class="s1">&#39;to_grayscale&#39;</span><span class="p">,</span> <span class="n">ObservationRGBToYFilter</span><span class="p">())</span>
<span class="n">DoomInputFilter</span><span class="o">.</span><span class="n">add_observation_filter</span><span class="p">(</span><span class="s1">&#39;observation&#39;</span><span class="p">,</span> <span class="s1">&#39;to_uint8&#39;</span><span class="p">,</span> <span class="n">ObservationToUInt8Filter</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">))</span>
<span class="n">DoomInputFilter</span><span class="o">.</span><span class="n">add_observation_filter</span><span class="p">(</span><span class="s1">&#39;observation&#39;</span><span class="p">,</span> <span class="s1">&#39;stacking&#39;</span><span class="p">,</span> <span class="n">ObservationStackingFilter</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>


<span class="n">DoomOutputFilter</span> <span class="o">=</span> <span class="n">OutputFilter</span><span class="p">(</span><span class="n">is_a_reference_filter</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">DoomOutputFilter</span><span class="o">.</span><span class="n">add_action_filter</span><span class="p">(</span><span class="s1">&#39;to_discrete&#39;</span><span class="p">,</span> <span class="n">FullDiscreteActionSpaceMap</span><span class="p">())</span>


<span class="k">class</span> <span class="nc">DoomEnvironmentParameters</span><span class="p">(</span><span class="n">EnvironmentParameters</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">level</span><span class="o">=</span><span class="n">level</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">default_input_filter</span> <span class="o">=</span> <span class="n">DoomInputFilter</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">default_output_filter</span> <span class="o">=</span> <span class="n">DoomOutputFilter</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cameras</span> <span class="o">=</span> <span class="p">[</span><span class="n">DoomEnvironment</span><span class="o">.</span><span class="n">CameraTypes</span><span class="o">.</span><span class="n">OBSERVATION</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">path</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;rl_coach.environments.doom_environment:DoomEnvironment&#39;</span>


<div class="viewcode-block" id="DoomEnvironment"><a class="viewcode-back" href="../../../components/environments/index.html#rl_coach.environments.doom_environment.DoomEnvironment">[docs]</a><span class="k">class</span> <span class="nc">DoomEnvironment</span><span class="p">(</span><span class="n">Environment</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">CameraTypes</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
        <span class="n">OBSERVATION</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;observation&quot;</span><span class="p">,</span> <span class="s2">&quot;screen_buffer&quot;</span><span class="p">)</span>
        <span class="n">DEPTH</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;depth&quot;</span><span class="p">,</span> <span class="s2">&quot;depth_buffer&quot;</span><span class="p">)</span>
        <span class="n">LABELS</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;labels&quot;</span><span class="p">,</span> <span class="s2">&quot;labels_buffer&quot;</span><span class="p">)</span>
        <span class="n">MAP</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;map&quot;</span><span class="p">,</span> <span class="s2">&quot;automap_buffer&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">level</span><span class="p">:</span> <span class="n">LevelSelection</span><span class="p">,</span> <span class="n">seed</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">frame_skip</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">human_control</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span>
                 <span class="n">custom_reward_threshold</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">],</span> <span class="n">visualization_parameters</span><span class="p">:</span> <span class="n">VisualizationParameters</span><span class="p">,</span>
                 <span class="n">cameras</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">CameraTypes</span><span class="p">],</span> <span class="n">target_success_rate</span><span class="p">:</span> <span class="nb">float</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param level: (str)</span>
<span class="sd">            A string representing the doom level to run. This can also be a LevelSelection object.</span>
<span class="sd">            This should be one of the levels defined in the DoomLevel enum. For example, HEALTH_GATHERING.</span>

<span class="sd">        :param seed: (int)</span>
<span class="sd">            A seed to use for the random number generator when running the environment.</span>

<span class="sd">        :param frame_skip: (int)</span>
<span class="sd">            The number of frames to skip between any two actions given by the agent. The action will be repeated</span>
<span class="sd">            for all the skipped frames.</span>

<span class="sd">        :param human_control: (bool)</span>
<span class="sd">            A flag that allows controlling the environment using the keyboard keys.</span>

<span class="sd">        :param custom_reward_threshold: (float)</span>
<span class="sd">            Allows defining a custom reward that will be used to decide when the agent succeeded in passing the environment.</span>

<span class="sd">        :param visualization_parameters: (VisualizationParameters)</span>
<span class="sd">            The parameters used for visualizing the environment, such as the render flag, storing videos etc.</span>

<span class="sd">        :param cameras: (List[CameraTypes])</span>
<span class="sd">            A list of camera types to use as observation in the state returned from the environment.</span>
<span class="sd">            Each camera should be an enum from CameraTypes, and there are several options like an RGB observation,</span>
<span class="sd">            a depth map, a segmentation map, and a top down map of the enviornment.</span>

<span class="sd">		:param target_success_rate: (float)</span>
<span class="sd">			Stop experiment if given target success rate was achieved.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">level</span><span class="p">,</span> <span class="n">seed</span><span class="p">,</span> <span class="n">frame_skip</span><span class="p">,</span> <span class="n">human_control</span><span class="p">,</span> <span class="n">custom_reward_threshold</span><span class="p">,</span> <span class="n">visualization_parameters</span><span class="p">,</span> <span class="n">target_success_rate</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">cameras</span> <span class="o">=</span> <span class="n">cameras</span>

        <span class="c1"># load the emulator with the required level</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">level</span> <span class="o">=</span> <span class="n">DoomLevel</span><span class="p">[</span><span class="n">level</span><span class="o">.</span><span class="n">upper</span><span class="p">()]</span>
        <span class="n">local_scenarios_path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)),</span> <span class="s1">&#39;doom&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="s1">&#39;COACH_LOCAL&#39;</span> <span class="ow">in</span> <span class="n">level</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">scenarios_dir</span> <span class="o">=</span> <span class="n">local_scenarios_path</span>
        <span class="k">elif</span> <span class="s1">&#39;VIZDOOM_ROOT&#39;</span> <span class="ow">in</span> <span class="n">environ</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">scenarios_dir</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;VIZDOOM_ROOT&#39;</span><span class="p">),</span> <span class="s1">&#39;scenarios&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">scenarios_dir</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="n">vizdoom</span><span class="o">.</span><span class="vm">__file__</span><span class="p">)),</span> <span class="s1">&#39;scenarios&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">game</span> <span class="o">=</span> <span class="n">vizdoom</span><span class="o">.</span><span class="n">DoomGame</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">load_config</span><span class="p">(</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">scenarios_dir</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">level</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">set_window_visible</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">add_game_args</span><span class="p">(</span><span class="s2">&quot;+vid_forcesurface 1&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">wait_for_explicit_human_action</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">human_control</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">set_screen_resolution</span><span class="p">(</span><span class="n">vizdoom</span><span class="o">.</span><span class="n">ScreenResolution</span><span class="o">.</span><span class="n">RES_640X480</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_rendered</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">set_screen_resolution</span><span class="p">(</span><span class="n">vizdoom</span><span class="o">.</span><span class="n">ScreenResolution</span><span class="o">.</span><span class="n">RES_320X240</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># lower resolution since we actually take only 76x60 and we don&#39;t need to render</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">set_screen_resolution</span><span class="p">(</span><span class="n">vizdoom</span><span class="o">.</span><span class="n">ScreenResolution</span><span class="o">.</span><span class="n">RES_160X120</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">set_render_hud</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">set_render_crosshair</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">set_render_decals</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">set_render_particles</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">camera</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cameras</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="p">,</span> <span class="s1">&#39;set_</span><span class="si">{}</span><span class="s1">_enabled&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">camera</span><span class="o">.</span><span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">])):</span>
                <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="p">,</span> <span class="s1">&#39;set_</span><span class="si">{}</span><span class="s1">_enabled&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">camera</span><span class="o">.</span><span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">]))(</span><span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">init</span><span class="p">()</span>

        <span class="c1"># actions</span>
        <span class="n">actions_description</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;NO-OP&#39;</span><span class="p">]</span>
        <span class="n">actions_description</span> <span class="o">+=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">action</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">action</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">get_available_buttons</span><span class="p">()]</span>
        <span class="n">actions_description</span> <span class="o">=</span> <span class="n">actions_description</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">action_space</span> <span class="o">=</span> <span class="n">MultiSelectActionSpace</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">get_available_buttons_size</span><span class="p">(),</span>
                                                   <span class="n">max_simultaneous_selected_actions</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
                                                   <span class="n">descriptions</span><span class="o">=</span><span class="n">actions_description</span><span class="p">,</span>
                                                   <span class="n">allow_no_action_to_be_selected</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="c1"># human control</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">human_control</span><span class="p">:</span>
            <span class="c1"># TODO: add this to the action space</span>
            <span class="c1"># map keyboard keys to actions</span>
            <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">action</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">action_space</span><span class="o">.</span><span class="n">descriptions</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">action</span> <span class="ow">in</span> <span class="n">key_map</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">key_to_action</span><span class="p">[(</span><span class="n">key_map</span><span class="p">[</span><span class="n">action</span><span class="p">],)]</span> <span class="o">=</span> <span class="n">idx</span>

        <span class="c1"># states</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">state_space</span> <span class="o">=</span> <span class="n">StateSpace</span><span class="p">({</span>
            <span class="s2">&quot;measurements&quot;</span><span class="p">:</span> <span class="n">VectorObservationSpace</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">get_state</span><span class="p">()</span><span class="o">.</span><span class="n">game_variables</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                                                   <span class="n">measurements_names</span><span class="o">=</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span>
                                                                       <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">get_available_game_variables</span><span class="p">()])</span>
        <span class="p">})</span>
        <span class="k">for</span> <span class="n">camera</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cameras</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">state_space</span><span class="p">[</span><span class="n">camera</span><span class="o">.</span><span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">ImageObservationSpace</span><span class="p">(</span>
                <span class="n">shape</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">get_screen_height</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">get_screen_width</span><span class="p">(),</span> <span class="mi">3</span><span class="p">]),</span>
                <span class="n">high</span><span class="o">=</span><span class="mi">255</span><span class="p">)</span>

        <span class="c1"># seed</span>
        <span class="k">if</span> <span class="n">seed</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">set_seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reset_internal_state</span><span class="p">()</span>

        <span class="c1"># render</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_rendered</span><span class="p">:</span>
            <span class="n">image</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_rendered_image</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">renderer</span><span class="o">.</span><span class="n">create_screen</span><span class="p">(</span><span class="n">image</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">image</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">target_success_rate</span> <span class="o">=</span> <span class="n">target_success_rate</span>

    <span class="k">def</span> <span class="nf">_update_state</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># extract all data from the current state</span>
        <span class="n">state</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">get_state</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">state</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">state</span><span class="o">.</span><span class="n">screen_buffer</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">measurements</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">game_variables</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">state</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;measurements&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">measurements</span><span class="p">}</span>
            <span class="k">for</span> <span class="n">camera</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cameras</span><span class="p">:</span>
                <span class="n">observation</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">camera</span><span class="o">.</span><span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">observation</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">state</span><span class="p">[</span><span class="n">camera</span><span class="o">.</span><span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">observation</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
                <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">observation</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">state</span><span class="p">[</span><span class="n">camera</span><span class="o">.</span><span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">repeat</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">observation</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="mi">3</span><span class="p">,</span> <span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">reward</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">get_last_reward</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">done</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">is_episode_finished</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_take_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">make_action</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">action</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">frame_skip</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_restart_environment_episode</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">force_environment_reset</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">game</span><span class="o">.</span><span class="n">new_episode</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_rendered_image</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a numpy array containing the image that will be rendered to the screen.</span>
<span class="sd">        This can be different from the observation. For example, mujoco&#39;s observation is a measurements vector.</span>
<span class="sd">        :return: numpy array containing the image that will be rendered to the screen</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">image</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">state</span><span class="p">[</span><span class="n">camera</span><span class="o">.</span><span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="k">for</span> <span class="n">camera</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cameras</span><span class="p">]</span>
        <span class="n">image</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">image</span>

    <span class="k">def</span> <span class="nf">get_target_success_rate</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_success_rate</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018-2019, Intel AI Lab

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>